package com.worldtolearn.offer.bu;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import com.worldtolearn.offer.dao.OfferDao;
import com.worldtolearn.offer.entity.Activity;
import com.worldtolearn.offer.entity.Offer;

@Named
public class OfferBu implements IOfferBu, Serializable {

	private static final long serialVersionUID = 1L;
	
	@Inject
	OfferDao offerDao;

	@Override
	public void createOffer(Offer offer) {
		System.out.println("OffreBu createOffer --");
		offerDao.createOffer(offer);
	}

	@Override
	public void deleteOffer(Offer offer) {
		offerDao.deleteOffer(offer);
	}

	@Override
	public List<Offer> getAllOffers() {
		return offerDao.getAllOffers();
	}

	@Override
	public List<Offer> getOfferByLabelOffer(String label) {
		return offerDao.getOfferByLabelOffer(label);
	}
	

	@Override
	public List<Offer> getOfferBySearchCriteria(String activity) {
		return offerDao.getOfferByActivity(activity);
	}
	
	@Override
	public List<Offer> getOfferBySearchCriterion(List<Offer> list, String[] criteria) {
		list.clear();
		list = offerDao.getOfferBySearchCriterion(criteria);
		return list;
	}
	
	@Override
	public List<Offer> getOfferByCountryAndActivity(List<Offer> offers, String pays, String activity){
		
		offers = getOfferByActivity(offers, activity);
		
		List<Offer> list = new ArrayList<Offer>();
		
		for(Offer o : offers){
			if(o.getSupplierOffer().getAdress().getCountry().equals(pays)){
				list.add(o);
			}
		}
		
		offers.clear();
		
		for(Offer o : list){
			offers.add(o);
		}		
		return offers;		
	}
	
	public List<Offer> getOfferBycountry(List<Offer>offers, String pays){
		offers.clear();
		offers = offerDao.getOfferByCountry(pays);
		return offers; 
	}
	
	public List<Offer> getOfferByActivity(List<Offer>offers, String activity) {
		offers.clear();
		offers = offerDao.getOfferByActivity(activity);
		return offers;	
	}
	

	public OfferDao getOfferDao() {
		return offerDao;
	}

	public void setOfferDao(OfferDao offerDao) {
		this.offerDao = offerDao;
	}

	@Override
	public List<Offer> getOfferById(int id) {
		return offerDao.getOfferById(id);
	}
}
